//
//  NSString+common.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/8/4.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "NSString+common.h"

@implementation NSString (common)

+ (NSAttributedString *)getInstallment:(CGFloat)installment nper:(NSInteger)nper font:(CGFloat)font {
    NSString *installmentPirce     = [NSString stringWithFormat:@"￥%@",[self getPreciseNumbers:installment]];
    
    NSString *period               = [NSString stringWithFormat:@"x%ld期",nper];
    
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@",installmentPirce, period]];
    [att addAttributes:@{NSForegroundColorAttributeName : HEXCOLOR(0x999999), NSFontAttributeName : [UIFont boldSystemFontOfSize:font]} range:NSMakeRange(installmentPirce.length+1, period.length)];
    
    return att;
}


+ (NSString *)getPreciseNumbers:(double)number {
    NSString *doubleStr = @"0";
    
    if (number) {
        doubleStr = [NSString stringWithFormat:@"%.2f", number];
    }
    
    NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:doubleStr];
    return [decNumber stringValue];
}

+ (NSString *)getcurrentDateWithFormat:(NSString *)format {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat       = format;
    return [dateFormatter stringFromDate:[NSDate date]];
}


- (CGFloat)adaptiveHeight:(CGFloat)width font:(CGFloat)font {
    if (NSStringNotNULL(self)) {
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName];
        
        CGRect bounds = [self boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        return bounds.size.height;
    }
    
    return 0;
}


- (CGFloat)adaptiveWidth:(CGFloat)height font:(CGFloat)font {
    if (NSStringNotNULL(self)) {
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName];
        
        CGRect bounds = [self boundingRectWithSize:CGSizeMake(0, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        return bounds.size.width;
    }
    
    return 0;
}


@end
